/*
 * @(#)CheckAndCreateTable.java   1.0  2021年11月8日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.dts.init;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.swift.core.env.ConfigInterface.ChangeConfigKV;
import com.swift.core.env.ConfigInterface.ChangeState;
import com.swift.core.env.ConfigInterface.ConfigChangeListener;
import com.swift.dao.db.datasource.DataSourceFactory;
import com.swift.dao.db.datasource.HandlerDataSource;
import com.swift.dts.dao.LogEventTableDao;
import com.swift.dts.init.core.DbChange;
import com.swift.dts.loadconfig.model.DtsDataBase;
import com.swift.util.type.ByteUtil;
import com.swift.util.type.TypeUtil;

/**
 * 检测需要应用到的表是否已被创建
 * 
 * @author zhengjiajin
 * @version 1.0 2021年11月8日
 */
@Component
@Order(1)
public class CheckAndCreateDbTable extends DbChange implements ConfigChangeListener<DtsDataBase> {

    private static final Logger log = LoggerFactory.getLogger(CheckAndCreateDbTable.class);
    @Autowired
    private LogEventTableDao logEventTableDao;

    @Autowired
    private DataSourceFactory dataSourceFactory;

    /**
     * @see com.swift.core.env.ConfigInterface.ConfigChangeListener#listener(java.util.List)
     */
    @Override
    public void listener(List<ChangeConfigKV<DtsDataBase>> list) {
        if (TypeUtil.isNull(list)) return;
        restartDataSource(list);
        restartTable(list);
    }

    private void restartTable(List<ChangeConfigKV<DtsDataBase>> list) {
        try {
            InputStream inputStream = this.getClass().getResourceAsStream("/sql/mysql/SwiftDtsLogEventTable.sql");
            String createTableSql = new String(ByteUtil.getInputStream(inputStream));
            inputStream.close();
            for (ChangeConfigKV<DtsDataBase> db : list) {
                DtsDataBase old = db.getOldConfig();
                DtsDataBase ne = db.getNewConfig();
                if (!checkAddressChange(old, ne) && !checkSourceChange(old, ne) && !checkRingChange(old, ne)) continue;
                if (!ne.getIsRing()) continue;
                if (ne.getIsSource()) continue;
                HandlerDataSource.putDataSource(ne.getKey());
                try {
                    logEventTableDao.createTable(createTableSql);
                    logEventTableDao.addPartition();
                    logEventTableDao.cleanPartition();
                } catch (Exception ex) {
                    log.error("异常:", ex);
                }
                HandlerDataSource.clear();
            }
        } catch (IOException e) {

        }

    }

    private void restartDataSource(List<ChangeConfigKV<DtsDataBase>> changeDbList) {
        for (ChangeConfigKV<DtsDataBase> db : changeDbList) {
            DtsDataBase old = db.getOldConfig();
            DtsDataBase ne = db.getNewConfig();
            if (!checkAddressChange(old, ne) && !checkPoolChange(old, ne)) continue;
            ChangeState state = this.getState(db);
            if (state == ChangeState.NEW) {
                dataSourceFactory.addDataSource(ne.getKey(), ne.getUrl(), ne.getUserName(), ne.getPassword(),
                    ne.getDriverClassName(), ne.getMaxPoolSize(), ne.getMinPoolSize());
            }

            if (state == ChangeState.DEL) {
                dataSourceFactory.delDataSource(old.getKey());
            }

            if (state == ChangeState.MODIFY) {
                //dataSourceFactory.delDataSource(old.getKey());
                dataSourceFactory.addDataSource(ne.getKey(), ne.getUrl(), ne.getUserName(), ne.getPassword(),
                    ne.getDriverClassName(), ne.getMaxPoolSize(), ne.getMinPoolSize());
            }
        }
    }

}
